﻿/**
*@description CheckBox树
*/
Chenxin.UI.CheckBoxTree = Chenxin.extend(null,{
    treeType:"checkbox",
    imgCheckBox:["/images/tree/iconCheckAll.gif","/images/tree/iconCheckGray.gif","/images/tree/iconUnCheckAll.gif","/images/tree/iconCheckDis.gif", "/images/tree/iconCheckDis.gif"],
    imgRadio:["/images/tree/radio_off.gif", "/images/tree/radio_on.gif"],
    imgLine:["/images/tree/line1.gif", "/images/tree/line2.gif", "/images/tree/line3.gif", "/images/tree/line4.gif"],
    imgMinus:["/images/tree/minus.gif", "/images/tree/minus2.gif", "/images/tree/minus3.gif", "/images/tree/minus4.gif", "/images/tree/minus5.gif"],
    imgPlus:["/images/tree/plus.gif", "/images/tree/plus2.gif", "/images/tree/plus3.gif", "/images/tree/plus4.gif", "/images/tree/plus5.gif"],    
    getTreeData:function(objRender,params){
        var objTree = this;
        var dataParams = params;
        if(params == undefined){
            dataParams = objTree.params;
        }else{
            Chenxin.apply(dataParams,objTree.params);
        }
        Chenxin.ajax({
			type: "post",
			dataType: "json",
			url: this.actionUrl,
			data: dataParams,
			onSuccess: function(data,msg) {
			    var jsonData = eval("(" + data + ")");
			    var strHtml = objTree.buildTree(jsonData,objRender);
			}
        });
    },
    buildTree:function(lsData,objRender){
        var strHtml="";
        var jsonData;
        var curCheckImg = this.imgCheckBox[2];
        var curPlusImg;
        if(objRender == undefined){
            strHtml = strHtml + "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"margin: 0pt; padding: 0pt; overflow: hidden;\"><tbody>";
        }
        if( lsData.length == 1){
            jsonData = lsData[0];
            strHtml = strHtml + "<tr>";
            if(objRender == undefined){
                strHtml = strHtml + "<td> </td>";
            }else{
                if(objRender.parents("tr:first").next().length == 0){
                    strHtml = strHtml + "<td> </td>";    
                }else{
                    strHtml = strHtml + "<td style=\"background-image: url(" + this.imgLine[0] + "); background-repeat: repeat-y;\"> </td>";
                }
                if(objRender.parents("tr:first").find("img:eq(1)").attr("src") == this.imgCheckBox[0]){
                    curCheckImg = this.imgCheckBox[0];
                }
            }
            strHtml = strHtml + "<td colspan=\"2\">";
            strHtml = strHtml + "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"margin: 0pt; padding: 0pt;\"><tbody>";
            
            strHtml = strHtml + "<tr id=\"" + jsonData.id + "\">";
            strHtml = strHtml + "<td width=\"20\" class=\"standartTreeImage\">";
            strHtml = strHtml + "<img class=\"clsImgPlus\" border=\"0\" align=\"absmiddle\" style=\"padding: 0pt; margin: 0pt; width: 18px; height: 18px;\" src=\"" + this.imgPlus[1] + "\" >";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "<td width=\"20\">";
            strHtml = strHtml + "<img class=\"clsImgObject\" align=\"absmiddle\" style=\"width: 16px; height: 16px;\" src=\"" + curCheckImg + "\" >";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "<td nowrap=\"\" class=\"standartTreeRow\" style=\"width: 100%; font-size: 10pt; cursor: pointer;\">";
            strHtml = strHtml + "<span class=\"standartTreeRow\" style=\"padding-left: 5px; padding-right: 5px;\">" + jsonData.text + "</span>";
            strHtml = strHtml + "</td>";
            strHtml = strHtml + "</tr>";
            
            strHtml = strHtml + "</tbody></table></td></tr>";
        }else{
            for(var idxCnt = 0; idxCnt < lsData.length; idxCnt++){
                jsonData = lsData[idxCnt];
                if( idxCnt == 0 ){
                    if(objRender == undefined){
                        curPlusImg = this.imgPlus[3];
                    }else{
                        curPlusImg = this.imgPlus[2];
                    }    
                }else if(idxCnt == lsData.length - 1){
                    curPlusImg = this.imgPlus[1];
                }else{
                    curPlusImg = this.imgPlus[2];
                }
                strHtml = strHtml + "<tr>";
                if(objRender == undefined){
                    strHtml = strHtml + "<td> </td>";
                }else{
                    if(objRender.parents("tr:first").next().length == 0){
                        strHtml = strHtml + "<td> </td>";    
                    }else{
                        strHtml = strHtml + "<td style=\"background-image: url(" + this.imgLine[0] + "); background-repeat: repeat-y;\"> </td>";
                    }
                    if(objRender.parents("tr:first").find("img:eq(1)").attr("src") == this.imgCheckBox[0]){
                        curCheckImg = this.imgCheckBox[0];
                    }
                }
                strHtml = strHtml + "<td colspan=\"2\">";
                strHtml = strHtml + "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"margin: 0pt; padding: 0pt;\"><tbody>";
            
                strHtml = strHtml + "<tr id=\"" + jsonData.id + "\">";
                strHtml = strHtml + "<td width=\"20px\" class=\"standartTreeImage\">";
                strHtml = strHtml + "<img class=\"clsImgPlus\" border=\"0\" align=\"absmiddle\" style=\"padding: 0pt; margin: 0pt; width: 18px; height: 18px;\" src=\"" + curPlusImg + "\" >";
                strHtml = strHtml + "</td>";
                strHtml = strHtml + "<td width=\"20px\">";
                strHtml = strHtml + "<img class=\"clsImgObject\" align=\"absmiddle\" style=\"width: 16px; height: 16px;\" src=\"" + curCheckImg + "\" >";
                strHtml = strHtml + "</td>";
                strHtml = strHtml + "<td nowrap=\"\" class=\"standartTreeRow\" style=\"width: 210px; font-size: 10pt; cursor: pointer;\">";
                strHtml = strHtml + "<span class=\"standartTreeRow\" style=\"padding-left: 5px; padding-right: 5px;\">" + jsonData.text + "</span>";
                strHtml = strHtml + "</td>";
                strHtml = strHtml + "</tr>";
                
                strHtml = strHtml + "</tbody></table></td></tr>";
             }
        }
        if(objRender == undefined){
            strHtml = strHtml + "</tbody></table>";
        }
        if(objRender == undefined){
	        $("#" + this.renderTo).html(strHtml);
	    }else{
	        objRender.after(strHtml);
	    }
    },
    innerEvent:function(){
        var objTree = this;
        $("#" + objTree.renderTo).click(function(event){
            
            //打开或关闭图标
            if($(event.target).hasClass("clsImgPlus")){
            
                var trParent = $(event.target).parents("tr:first");
			    objTree.toggleState(trParent);
			//选择框点击事件
            }else if($(event.target).hasClass("clsImgObject")){
            
                var trParent = $(event.target).parents("tr:first");
                objTree.setDescent(trParent);
                objTree.setAncestor(trParent);
            }
            
            //文字点击事件
            if(event.target.tagName == "SPAN"){
                $("#" + objTree.renderTo).find("span").removeClass("selectedTreeRow");
                $("#" + objTree.renderTo).find("span").addClass("standartTreeRow");
                $(event.target).removeClass("standartTreeRow");
                $(event.target).addClass("selectedTreeRow");
            }
        });
        $("#" + objTree.renderTo).dblclick(function(event){
        
            //文字点击事件
            if(event.target.tagName == "SPAN"){
                var trParent = $(event.target).parents("tr:first");
                objTree.toggleState(trParent);
            }
        });
    },
    clearChkState:function(trParent){
        if(trParent == undefined){
            $("#" + this.renderTo)
        }
    },
    setDescent:function(ParentNode,ImgName){
        var Children = $(ParentNode).nextAll();
        var objImg = $(ParentNode).find("img:eq(1)");
        var imgChild;
        if(ImgName != undefined){
            objImg.attr("src",ImgName);
            imgChild = ImgName;
        }else{
            var curImg = objImg.attr("src");
            switch(curImg){
                //全选
                case this.imgCheckBox[0]:
                    objImg.attr("src",this.imgCheckBox[2]);
                    imgChild = this.imgCheckBox[2];
                    break;
                //半选
                case this.imgCheckBox[1]:
                    objImg.attr("src",this.imgCheckBox[0]);
                    imgChild = this.imgCheckBox[0];
                    break;
                //没选
                case this.imgCheckBox[2]:
                    objImg.attr("src",this.imgCheckBox[0]);
                    imgChild = this.imgCheckBox[0];
                    break;
            }
        }
        //循环子节点
        for(var idxCnt = 0; idxCnt < Children.length; idxCnt++ ){
            this.setDescent($(Children[idxCnt]).find("tr:first"),imgChild);
        }
    },
    setAncestor:function(ChildNode){
        var ParentLevel = $(ChildNode).parent("tbody").parent("table").parent("td").parent("tr").parent("tbody");
        var Parents = ParentLevel.children("tr");
        if(ParentLevel.parent("table").parent().attr("id") == this.renderTo){
            return;
        }
        var Total = 0;
        var curImg;
        for(var idxCnt = 1;idxCnt < Parents.length; idxCnt++ ){
            curImg = $(Parents[idxCnt]).find("img:eq(1)").attr("src");
            switch(curImg){
                //全选
                case this.imgCheckBox[0]:
                    Total = Total + 1;
                    break;
                //半选
                case this.imgCheckBox[1]:
                    Total = Total + 0.5;
                    break;
                //没选
                case this.imgCheckBox[2]:
                    Total = Total + 0;
                    break;
            }
        }
        
        if(Total == Parents.length - 1){
            $(Parents[0]).find("img:eq(1)").attr("src",this.imgCheckBox[0]);
        }else if(Total == 0){
            $(Parents[0]).find("img:eq(1)").attr("src",this.imgCheckBox[2]);
        }else{
            $(Parents[0]).find("img:eq(1)").attr("src",this.imgCheckBox[1]);
        }
        
        this.setAncestor(Parents[0]);
    },
	toggleState:function(trParent){
	    var objImg = trParent.find("img:first");
		var curImg = trParent.find("img:first").attr("src");
		switch(curImg){
			case this.imgPlus[4]:
				$(objImg).attr("src",this.imgMinus[4]);
				this.showChildren(trParent);
				break;
			case this.imgPlus[3]:
				$(objImg).attr("src",this.imgMinus[3]);
				this.showChildren(trParent);
				break;
			case this.imgPlus[2]:
				$(objImg).attr("src",this.imgMinus[2]);
				this.showChildren(trParent);
				break;
			case this.imgPlus[1]:
				$(objImg).attr("src",this.imgMinus[1]);
				this.showChildren(trParent);
				break;
			case this.imgMinus[4]:
				$(objImg).attr("src",this.imgPlus[4]);
				this.hideChildren(trParent);
				break;
			case this.imgMinus[3]:
				$(objImg).attr("src",this.imgPlus[3]);
				this.hideChildren(trParent);
				break;
			case this.imgMinus[2]:
				$(objImg).attr("src",this.imgPlus[2]);
				this.hideChildren(trParent);
				break;
			case this.imgMinus[1]:
				$(objImg).attr("src",this.imgPlus[1]);
				this.hideChildren(trParent);
				break;
		}
	},
    showChildren:function(trParent){
        
        var objNext = trParent.next();
        //有子树
        if(objNext.length>0){
            this.setChildren(trParent,true);
        }else{
            var params = {};
            params.id = trParent.attr("id");
            this.getTreeData(trParent,params);
        }
    },
    hideChildren:function(trParent){
        this.setChildren(trParent,false);
    },
    setChildren:function(trParent,Status){
        if(Status == false){
            $(trParent.nextAll()).hide();
        }else{
            $(trParent.nextAll()).show();
        }
    },
    getSelectedValNm:function(){
        var lsRet = this.getChildrenValNm($("#"+this.renderTo).find("tbody:first"));
        return lsRet;
    },
    getChildrenValNm:function(Parent){
        var lsValNm=[];
        var jsonValNm={};
        var lsValNmChd;
        var trParents = Parent.children("tr");
        var preNode = trParents[0];
        if( $(preNode).attr("id") != "" ){
            switch( $(preNode).children("td:eq(1)").find("img:first").attr("src")){
                //全选
                case this.imgCheckBox[0]:
                    jsonValNm.id = $(preNode).attr("id");
                    jsonValNm.name = $(preNode).children("td:eq(2)").find("span:first").text();
                    lsValNm.push(jsonValNm);
                    break;
                //半选
                case this.imgCheckBox[1]:
                    for( var idxCnt=1; idxCnt < trParents.length; idxCnt++ ){
                        if( $(trParents[idxCnt]).find("tbody:first") != undefined ){
                            lsValNmChd = this.getChildrenValNm($(trParents[idxCnt]).find("tbody:first"));
                            if( lsValNmChd.length > 0 ){
                               lsValNm =  lsValNm.concat(lsValNmChd);
                            }
                        }
                    }
                    break;
                //没选
                case this.imgCheckBox[2]:
                    break;
            }
        }else{
            for( var idxCnt=0; idxCnt < trParents.length; idxCnt++ ){
                if( $(trParents[idxCnt]).find("tbody:first") != undefined ){
                    lsValNmChd = this.getChildrenValNm($(trParents[idxCnt]).find("tbody:first"));
                    if( lsValNmChd.length > 0 ){
                       lsValNm = lsValNm.concat(lsValNmChd);
                    }
                }
            }
        }
        return lsValNm;
    },
    reload:function(){
        this.getTreeData();
    },
    initComponent:function(){
        this.getTreeData();
        this.innerEvent();
    }
});